package org.smr.ministore.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.blade.dao.base.BaseMapper;
import org.blade.service.base.AbstractService;
import org.blade.utils.IdGenUtils;
import org.blade.utils.Pager;

import org.smr.ministore.dao.DictionaryMapper;
import org.smr.ministore.dao.DictionaryOrganMapper;
import org.smr.ministore.entities.Dictionary;
import org.smr.ministore.entities.DictionaryGroupQueryVO;
import org.smr.ministore.entities.DictionaryOrgan;
import org.smr.ministore.service.DictionaryService;

import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("DictionaryService")
@Transactional(readOnly = false)
public class DictionaryServiceImpl extends AbstractService<Long, Dictionary> implements DictionaryService {

	@Autowired
	private DictionaryMapper mapper;

	@Autowired
	private DictionaryOrganMapper dictionaryOrganMapper;

	public DictionaryServiceImpl(DictionaryMapper mapper) {
		super(mapper);
	}

	public int save(Dictionary entity) {
		this.saveAttatch(entity);

		int effectCount = 0;
		effectCount += mapper.insert(entity);
		DictionaryOrgan dictionaryOrgan = new DictionaryOrgan();
		IdGenUtils.saveAttatch(dictionaryOrgan);
		dictionaryOrgan.setBelongToOrganId(entity.getOrganId());
		dictionaryOrgan.setDictionaryId(entity.getId());
		dictionaryOrgan.setOrganPath(entity.getOrganPath());
		dictionaryOrgan.setOrganId(entity.getOrganId());

		effectCount += dictionaryOrganMapper.insert(dictionaryOrgan);
		return effectCount;
	}

	public int updateById(Dictionary id) {
		super.modifyAttach(id);
		return mapper.updateById(id);
	}

    @Transactional(readOnly = true)
    public Dictionary queryById(Long id) {
		return mapper.selectById(id);
	}

	public int deleteById(Long id) {
		return mapper.deleteById(id);
	}

	public int deleteByIdLogical(Long id) {
		return 0;
	}

	@Transactional(readOnly = true)
    public Pager<Dictionary> paginate( Dictionary entity, Long pageSize, Long pageNum) {

		Pager<Dictionary> pager = new Pager( pageSize, pageNum );
		List list  = mapper.pagination(entity, pager);
		pager.setEntities(list);
		return pager;
    }
	
	public int batchSave(List<Dictionary> entities) {
		this.batchSaveAttatch(entities);
		return mapper.batchInsert(entities);
	}

	public int batchModify(List<Dictionary> entities) {
	    this.batchModifyAttatch(entities);
		return mapper.batchInsert(entities);
	}

	@Transactional(readOnly = true)
    public List<Dictionary> getDictionaryGroup(int sys, String bigType, Long organId, String organPath) {

		return this.mapper.getDictionaryGroup(sys,bigType,organId,organPath);
	}

    @Transactional(readOnly = true)
    public List<Dictionary> getDictionaryGroup(DictionaryGroupQueryVO query){

		int sys = query.getSys();
		String bigType = query.getBigType();
		Long organId = query.getOrganId();
		String organPath = query.getOrganPath();

        return this.mapper.getDictionaryGroup(sys,bigType,organId,organPath);

    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getReferencedDictionaryList(Map<String, Object> params) {
        int sys = MapUtils.getIntValue(params,"sys");
        String bigType = MapUtils.getString(params,"bigType");
        Long organId = MapUtils.getLong(params,"organId");
        Long originId = MapUtils.getLong(params,"originId");

        return this.mapper.getReferencedDictionaryList(sys,organId,bigType,originId);
	}

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getReferencedDictionaryList(int sys, Long organId, String bigType, Long originId) {


		List<Map<String,Object>> referencedDictionaryList =  this.mapper.
                getReferencedDictionaryList(sys,organId,bigType,originId);

		for(Map referencedDictionary : referencedDictionaryList){

            String id = MapUtils.getString(referencedDictionary, "id");
            String parentId = MapUtils.getString(referencedDictionary, "parentId");

            referencedDictionary.put("id", id);
            referencedDictionary.put("parentId", parentId);
        }
	    return referencedDictionaryList;
	}

    public  Pager<Dictionary> getOrganReferencedDictionaryPagination(Dictionary entity, Long pageSize, Long pageNum) {
        Pager<Dictionary> pager = new Pager( pageSize, pageNum );
        List list  = mapper.getOrganReferencedDictionaryPagination(entity, pager);
        pager.setEntities(list);
        return pager;
    }

	public List<Dictionary> getOrganReferencedDictionary(Dictionary entity) {
		return mapper.getOrganReferencedDictionary(entity);
	}

	public Long getId() {
		return IdGenUtils.getNextLongId();
	}
}
